﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class DonViBLL
    {
        ChamCongEntities cc = new ChamCongEntities();
        public List<DonVi> GetAllDonVi()
        {
            return cc.DonVis.ToList<DonVi>();
        }

        public DonVi GetDonViById(int id)
        {
            return cc.DonVis.Where(cd => cd.MaDonVi == id).FirstOrDefault();
        }

        public List<DonVi> SearchByTen(string key)
        {
            using (var context = new ChamCongEntities())
            {
                var idParam = new SqlParameter
                {
                    ParameterName = "kytu",
                    Value = key
                };

                return context.Database.SqlQuery<DonVi>("exec SearchByTenInDonVi @kytu", idParam).ToList<DonVi>();
            }
        }

        public void InsertDonVi(DonVi donVi)
        {
            cc.Entry(donVi).State = EntityState.Added;
            cc.SaveChanges();
        }
        public void UpdateDonVi(DonVi donVi)
        {
            cc.Entry(donVi).State = EntityState.Modified;
            cc.SaveChanges();
        }
        public bool DeleteDonVi(DonVi donVi)
        {
            cc.Entry(donVi).State = EntityState.Deleted;
            return cc.SaveChanges() > 0;
        }
    }
}
